package life.system.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import life.system.common.dto.MemberResponseVo;
import life.system.order.entity.OrderEntity;
import life.system.order.entity.OrderItemEntity;
import life.system.order.interceptor.LoginUserInterceptor;
import life.system.order.service.OrderItemService;
import life.system.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import life.system.common.utils.PageUtils;
import life.system.common.utils.Query;

import life.system.order.dao.OrderReturnApplyDao;
import life.system.order.entity.OrderReturnApplyEntity;
import life.system.order.service.OrderReturnApplyService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderReturnApplyService")
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyDao, OrderReturnApplyEntity> implements OrderReturnApplyService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderReturnApplyEntity> page = this.page(
                new Query<OrderReturnApplyEntity>().getPage(params),
                new QueryWrapper<OrderReturnApplyEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 订单退款
     */
    @Transactional
    public void orderReturn(String orderSn) {

        // 获取当前登录的用户
        String wxToken = LoginUserInterceptor.loginUser.get();
        String memberJson = (String) redisTemplate.boundHashOps("token:" + wxToken).get(wxToken);
        MemberResponseVo memberResponseVo = JSONObject.parseObject(memberJson, MemberResponseVo.class);

        OrderEntity order = orderService.getOrderByOrderSn(orderSn);
        order.setStatus(3);
        order.setQcCode("");
        orderService.updateById(order);

        // 2、根据orderSn 获取订单项数据
        OrderItemEntity orderItem = orderItemService.getOne(new QueryWrapper<OrderItemEntity>().eq("order_sn", orderSn));

        OrderReturnApplyEntity returnApply = new OrderReturnApplyEntity();

        returnApply.setSkuId(orderItem.getSkuId());
        returnApply.setOrderSn(orderSn);
        returnApply.setCreateTime(new Date());
        returnApply.setMemberUsername(memberResponseVo.getNickname());
        returnApply.setReturnAmount(orderItem.getRealAmount());
        returnApply.setStatus(2);
        returnApply.setHandleTime(new Date());
        returnApply.setSkuImg(orderItem.getSkuPic());
        returnApply.setSkuName(orderItem.getSkuName());
        returnApply.setSkuCount(orderItem.getSkuQuantity());
        returnApply.setSkuPrice(orderItem.getSkuPrice());
        returnApply.setSkuRealPrice(orderItem.getRealAmount());

        this.save(returnApply);

    }

}